#include "FMyAssetThumbnail.h"
#include "../Private/PackageReader.h"
#include "AssetData.h"

FMyAssetThumbnail::FMyAssetThumbnail(FString PackageFilename, uint32 InWidth, uint32 InHeight)
	:Width(InWidth)
	, Height(InHeight)
	, ThumbnailTexture(NULL)
{
	DataInit(PackageFilename);
}

FMyAssetThumbnail::~FMyAssetThumbnail()
{

}

void FMyAssetThumbnail::DataInit(FString PackageFilename)
{
	FPackageReader PackageReader;
	FPackageReader::EOpenPackageResult OpenPackageResult;
	TArray<FString> CookedPackageNamesWithoutAssetData;
	TArray<FAssetData*> AssetDataList;
	if (!PackageReader.OpenPackageFile(PackageFilename,&OpenPackageResult))
	{
		return;
	}

	if (PackageReader.ReadAssetRegistryDataIfCookedPackage(AssetDataList, CookedPackageNamesWithoutAssetData))
	{
		return;
	}

	if (!PackageReader.ReadAssetRegistryData(AssetDataList))
	{
		if (!PackageReader.ReadAssetDataFromThumbnailCache(AssetDataList))
		{
			return;
		}
	}
	_AssetData = AssetDataList[0];

}

FIntPoint FMyAssetThumbnail::GetSize()  const
{
	return FIntPoint(Width, Height);
}

FSlateShaderResource* FMyAssetThumbnail::GetViewportRenderTargetTexture() const
{
	//FSlateTexture2DRHIRef* Texture = NULL;
	//if (ThumbnailPool.IsValid())
	//{
	//	Texture = ThumbnailPool.Pin()->AccessTexture(AssetData, Width, Height);
	//}
	//if (!Texture || !Texture->IsValid())
	//{
	//	return NULL;
	//}
	return NULL;
}
//
//UObject* FMyAssetThumbnail::GetAsset() const
//{
//	//if (_AssetData.ObjectPath != NAME_None)
//	//{
//	//	return FindObject<UObject>(NULL, *_AssetData.ObjectPath.ToString());
//	//}
//	//else
//	{
//		return NULL;
//	}
//}
//
//const FAssetData& FMyAssetThumbnail::GetAssetData() const
//{
//	return _AssetData;
//}
//
//void FMyAssetThumbnail::SetAsset(const UObject* InAsset)
//{
//	SetAsset(FAssetData(InAsset));
//}
//
//void FMyAssetThumbnail::SetAsset(const FAssetData& InAssetData)
//{
//	if (InAssetData.IsValid())
//	{
//		_AssetData = InAssetData;
//	}
//	else
//	{
//		_AssetData = FAssetData();
//	}
//}
